package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.service.Gwat110Service;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 控制类
 * Created by wenchuan on 2018/4/27.
 */
@RestController
@RequestMapping("api/wx/gwat110")
public class Gwat110Controller extends ControllerSupport {
    @Autowired
    Gwat110Service gwat110Service;

    /**
     * 获取意向商机询单
     *
     * @return
     */
    @RequestMapping(value = "getGwcmDatas")
    public ResponseData getGwcmDatas(@DtoParam DtoWapper wapper) {
        Dto inDto =wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser user = wapper.getSysUser();
        try {
            inDto.put("GWCM008", user.getACCOUNT());
            list = gwat110Service.getGwcmData(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 获取潜在意向商机询单ID
     *
     * @return
     */
    @RequestMapping(value = "getGwagDatas")
    public ResponseData getGwagDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getStarData(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取已审核
     *
     * @return
     */
    @RequestMapping(value = "getChecked")
    public ResponseData getChecked(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getChecked(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取已购物
     *
     * @return
     */
    @RequestMapping(value = "getYgw")
    public ResponseData getYgw(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getYgw(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取已下单
     *
     * @return
     */
    @RequestMapping(value = "getYxd")
    public ResponseData getYxd(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getYxd(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取已发运
     *
     * @return
     */
    @RequestMapping(value = "getYfy")
    public ResponseData getYfy(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getYfy(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取已安装
     *
     * @return
     */
    @RequestMapping(value = "getYaz")
    public ResponseData getYaz(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getYaz(inDto);
        return SUCCESS(list);
    }

    /**
     * 获取已评价
     *
     * @return
     */
    @RequestMapping(value = "getYpj")
    public ResponseData getYpj(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat110Service.getYpj(inDto);
        return SUCCESS(list);
    }

    /**
     * 修改状态
     *
     * @return
     */
    @RequestMapping("updateData")
    public ResponseData updateData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            JSONArray characteristics = inJson.getJSONArray("CHARACTERISTICS");
            for(int i = 0; i < characteristics.size(); i++){
                JSONObject item = characteristics.getJSONObject(i);
                Dto dto = new Dto();
                dto.put("GWCMACTI",item.getString("GWCMACTI"));
                dto.put("GWCMENT",item.getString("GWCMENT"));
                dto.put("GWCMDOCNO",item.getString("GWCMDOCNO"));
                dto.put("GWCMSEQ",item.getString("GWCMSEQ"));
                dto.put("GWCMDATE", DateUtils.getDate("yyyy-MM-dd"));
                gwat110Service.update(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 批量修改状态
     *
     * @return
     */
    @RequestMapping("update")
    public ResponseData update(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            JSONArray _list = inJson.getJSONArray("_list");
            for(int i = 0; i < _list.size(); i++){
                JSONObject item = _list.getJSONObject(i);
                JSONObject inJson2 = JSONObject.parseObject(item.toJSONString());
                JSONArray characteristics = inJson2.getJSONArray("CHARACTERISTICS");
                for(int j = 0; j < characteristics.size(); j++){
                    JSONObject item2 = characteristics.getJSONObject(j);
                    Dto dto = new Dto();
                    dto.put("GWCMACTI",item2.getString("GWCMACTI"));
                    dto.put("GWCMENT",item2.getString("GWCMENT"));
                    dto.put("GWCMDOCNO",item2.getString("GWCMDOCNO"));
                    dto.put("GWCMSEQ",item2.getString("GWCMSEQ"));
                    gwat110Service.update(dto);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

}
